<template>
  <div class="app-container">
      <el-form :model="queryParams" ref="queryForm" size="small"
             :inline="true" v-show="showSearch" label-width="68px"
             style="display: flex;flex-direction: row;flex-wrap: wrap;justify-content: space-between;">
      <el-form-item label="菜品名称" prop="dishname">
        <el-input
          v-model="queryParams.dishname"
          placeholder="请输入菜品名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="餐厅ID" prop="categoryid">
        <el-input
          v-model="queryParams.categoryid"
          placeholder="请输入餐厅ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="参考图片" prop="picture">
        <el-input
          v-model="queryParams.picture"
          placeholder="请输入参考图片"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="价格" prop="price">
        <el-input
          v-model="queryParams.price"
          placeholder="请输入价格"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="剩余数量" prop="quantity">
        <el-input
          v-model="queryParams.quantity"
          placeholder="请输入剩余数量"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="描述" prop="description">
        <el-input
          v-model="queryParams.description"
          placeholder="请输入描述"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="口味1" prop="option1">
        <el-input
          v-model="queryParams.option1"
          placeholder="请输入口味1"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="口味2" prop="option2">
        <el-input
          v-model="queryParams.option2"
          placeholder="请输入口味2"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="口味3" prop="option3">
        <el-input
          v-model="queryParams.option3"
          placeholder="请输入口味3"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="口味4" prop="option4">
        <el-input
          v-model="queryParams.option4"
          placeholder="请输入口味4"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="类别" prop="species">
        <el-input
          v-model="queryParams.species"
          placeholder="请输入类别"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否推荐" prop="recommend">
        <el-input
          v-model="queryParams.recommend"
          placeholder="请输入是否推荐"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['operation:dishoperating:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['operation:dishoperating:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['operation:dishoperating:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['operation:dishoperating:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="dishoperatingList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="菜品ID" align="center" prop="dishid" />
      <el-table-column label="菜品名称" align="center" prop="dishname" />
      <el-table-column label="餐厅ID" align="center" prop="categoryid" />
      <el-table-column
        label="菜品图片"
        prop="picture"
        align="center"
      >
        <template slot-scope="scope">
          <el-image
            style="width: 80px; height: 80px;"
            :src="scope.row.picture"
          >
          </el-image>
        </template>
      </el-table-column>
      <!-- <el-table-column label="参考图片" align="center" prop="picture" /> -->
      <el-table-column label="价格" align="center" prop="price" />
      <el-table-column label="剩余数量" align="center" prop="quantity" />
      <el-table-column label="描述" align="center" prop="description" />
      <el-table-column label="口味1" align="center" prop="option1" />
      <el-table-column label="口味2" align="center" prop="option2" />
      <el-table-column label="口味3" align="center" prop="option3" />
      <el-table-column label="口味4" align="center" prop="option4" />
       <el-table-column label="审核状态" align="center" >
        <template slot-scope="status">
          {{ status.row.date==0?"通过":"未通过" }}
        </template>
      </el-table-column>
      <!-- <el-table-column label="审核状态" align="center" prop="status" /> -->
      <el-table-column label="类别" align="center" prop="species" />
      <el-table-column label="是否推荐" align="center" prop="recommend" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['operation:dishoperating:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['operation:dishoperating:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改商户菜品管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="菜品名称" prop="dishname">
          <el-input v-model="form.dishname" placeholder="请输入菜品名称" />
        </el-form-item>
        <el-form-item label="餐厅ID" prop="categoryid">
          <el-input v-model="form.categoryid" placeholder="请输入餐厅ID" />
        </el-form-item>
        <el-form-item label="菜品图片" prop="picture">
            <template slot-scope="scope">
              <el-upload
                class="avatar-uploader"
                action="http://localhost:8082/codeback/garbage/getPicture/"
                :show-file-list="false"
                :on-success="handleAvatarSuccess"
                :before-upload="beforeAvatarUpload">
                <img v-if="imageUrl" :src="imageUrl" class="avatar">
                <i v-else class="el-icon-plus avatar-uploader-icon"></i>
              </el-upload>
            </template>
        </el-form-item>
        <el-form-item label="价格" prop="price">
          <el-input v-model="form.price" placeholder="请输入价格" />
        </el-form-item>
        <el-form-item label="剩余数量" prop="quantity">
          <el-input v-model="form.quantity" placeholder="请输入剩余数量" />
        </el-form-item>
        <el-form-item label="描述" prop="description">
          <el-input v-model="form.description" placeholder="请输入描述" />
        </el-form-item>
        <el-form-item label="口味1" prop="option1">
          <el-input v-model="form.option1" placeholder="请输入口味1" />
        </el-form-item>
        <el-form-item label="口味2" prop="option2">
          <el-input v-model="form.option2" placeholder="请输入口味2" />
        </el-form-item>
        <el-form-item label="口味3" prop="option3">
          <el-input v-model="form.option3" placeholder="请输入口味3" />
        </el-form-item>
        <el-form-item label="口味4" prop="option4">
          <el-input v-model="form.option4" placeholder="请输入口味4" />
        </el-form-item>
        <el-form-item label="类别" prop="species">
          <el-input v-model="form.species" placeholder="请输入类别" />
        </el-form-item>
        <el-form-item label="是否推荐" prop="recommend">
          <el-input v-model="form.recommend" placeholder="请输入是否推荐" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listDishoperating, getDishoperating, delDishoperating, addDishoperating, updateDishoperating } from "@/api/operation/dishoperating";

export default {
  name: "Dishoperating",
  data() {
    return {
      imageUrl: '',
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 商户菜品管理表格数据
      dishoperatingList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        dishname: null,
        categoryid: null,
        picture: null,
        price: null,
        quantity: null,
        description: null,
        option1: null,
        option2: null,
        option3: null,
        option4: null,
        status: null,
        species: null,
        recommend: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        dishname: [
          { required: true, message: "菜品名称不能为空", trigger: "blur" }
        ],
        categoryid: [
          { required: true, message: "餐厅ID不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    handleAvatarSuccess(res, file) {
      this.imageUrl = URL.createObjectURL(file.raw);
      console.log(this.imageUrl);
    },
    beforeAvatarUpload(file) {
      const isPNG = file.type === 'image/png';
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isPNG) {
        this.$message.error('上传头像图片只能是 PNG 格式!');
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!');
      }
      return isPNG && isLt2M;
    },
    /** 查询商户菜品管理列表 */
    getList() {
      this.loading = true;
      listDishoperating(this.queryParams).then(response => {
        this.dishoperatingList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        dishid: null,
        dishname: null,
        categoryid: null,
        picture: null,
        price: null,
        quantity: null,
        description: null,
        option1: null,
        option2: null,
        option3: null,
        option4: null,
        status: 0,
        species: null,
        recommend: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.dishid)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加商户菜品管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const dishid = row.dishid || this.ids
      getDishoperating(dishid).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改商户菜品管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.dishid != null) {
            updateDishoperating(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDishoperating(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const dishids = row.dishid || this.ids;
      this.$modal.confirm('是否确认删除商户菜品管理编号为"' + dishids + '"的数据项？').then(function() {
        return delDishoperating(dishids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('operation/dishoperating/export', {
        ...this.queryParams
      }, `dishoperating_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
